﻿using System;
using System.IO;
using ProjScan.Config;
using ProjScan.RecordManager;
using ProjScan.ScanRule;
using ProjScan.ScanRule.Attr;
using ProjScan.Submodules.Scene_Check;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.Modules
{
	// Token: 0x0200030F RID: 783
	internal class ScenesCheckModule : BaseModules<ScenesCheckModule, ScenesCheckConfig>
	{
		// Token: 0x0600127D RID: 4733 RVA: 0x00057148 File Offset: 0x00055348
		public override void Run(StreamWriter ruleStreamWriter)
		{
			Singleton<PerformanceMeasurer>.Instance.PushState("Scene_Check");
			ScanLog.Instance.LogInfo(Module.Scenes_Check);
			this.rulesSw = ruleStreamWriter;
			this.Init();
			this.LoadRules(LoadMode.Check);
			bool flag = this.baseRules_Check == null || this.baseRules_Check.Count == 0;
			if (flag)
			{
				ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s2, "Rules is empty");
				ScanLog.UnityLogError("场景检测运行失败：相关规则全部关闭");
				this.Close();
			}
			else
			{
				RecordManagerFactory.Instance.RecordRuleInit(this.recordRules_Check);
				foreach (RecordRule recordRule in this.recordRules_Check)
				{
					recordRule.CreateNewOutputFile();
				}
				foreach (GlobalRule globalRule in this.globalRules_Check)
				{
					globalRule.CreateNewOutputFile();
				}
				int i = 0;
				while (i < ScenesMapData.Instance.scenesList.Length)
				{
					ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s3, string.Format("Scene({0})", i), ScenesMapData.Instance.scenesList[i]);
					try
					{
						ScenesMapData.Instance.LoadScene(ScenesMapData.Instance.scenesList[i]);
					}
					catch (Exception ex)
					{
						ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, "Open Scene", string.Concat(new string[]
						{
							(ex.GetType() == null) ? null : ex.GetType().ToString(),
							"\n",
							ex.Message,
							"\n",
							ex.StackTrace
						}));
						Debug.LogError("Proj Scan提示：场景检测失败 - 无法打开场景 @" + ScenesMapData.Instance.scenesList[i] + "。 请检查该场景是否存在。");
						goto IL_3D5;
					}
					goto IL_1E3;
					IL_3D5:
					i++;
					continue;
					IL_1E3:
					this.InitRules(1000 + i * this.baseRules_Check.Count);
					bool flag2 = this.recordRules_Check.Count > 0;
					if (flag2)
					{
						try
						{
							RecordManagerFactory.Instance.PrepareAssets();
						}
						catch (Exception ex2)
						{
							ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Scenes_Check, ScanLog.Stage.s2, "ScanAll", string.Concat(new string[]
							{
								ex2.GetType().FullName,
								" ",
								ex2.Message,
								"\n",
								ex2.StackTrace
							}));
							this.Close();
							return;
						}
						for (int j = 0; j < this.recordRules_Check.Count; j++)
						{
							RecordRule recordRule2 = this.recordRules_Check[j];
							recordRule2.SceneId = new int?(ScenesMapData.Instance.Index);
							recordRule2.OutputData(true);
							recordRule2.OutputDetailData();
							recordRule2.DumpToRuleCSV(ref this.rulesSw);
						}
						ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s4, "Done.");
						RecordManagerFactory.Instance.OutPutScanScr();
						ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s5, "Done.");
						RecordManagerFactory.Instance.Close();
					}
					Singleton<SceneStatisticsCollector>.Instance.GatherData();
					ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s3, "GlobalRulesRun.");
					foreach (GlobalRule globalRule2 in this.globalRules_Check)
					{
						globalRule2.SceneId = new int?(ScenesMapData.Instance.Index);
						globalRule2.Run();
						globalRule2.OutputData(true);
						globalRule2.OutputDetailData();
						globalRule2.DumpToRuleCSV(ref this.rulesSw);
					}
					ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s4, "Done.");
					Singleton<SceneStatisticsCollector>.Instance.ClearData();
					goto IL_3D5;
				}
				this.Close();
			}
		}

		// Token: 0x0600127E RID: 4734 RVA: 0x00057594 File Offset: 0x00055794
		public override void Init()
		{
			this.config = (Singleton<ConfigMgr>.Instance.GetConfig<ScenesCheckConfig>() as ScenesCheckConfig);
			ScenesMapData.Instance.Process();
			RecordManagerFactory.Instance.Init();
			this.baseRules_Check.Clear();
			this.recordRules_Check.Clear();
			this.globalRules_Check.Clear();
			this.ruleAttr = typeof(ScenesCheckRuleAttr);
		}

		// Token: 0x0600127F RID: 4735 RVA: 0x00008A53 File Offset: 0x00006C53
		public override void Close()
		{
			ScenesMapData.Instance.Output();
			Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("ScenesCheckEnd");
			ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s6, "Done.");
		}
	}
}
